home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / real.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  4.1 KB  |  194 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class  REAL
  5. --
  6. -- Note : corresponding C type is "float"
  7. --
  8. inherit
  9.    REAL_REF
  10.       redefine
  11.      infix "+", infix "-", infix "*", infix "/", infix "^",
  12.      prefix "+", prefix "-", valid_divisor, infix "<", 
  13.      infix "<=", infix ">", infix ">=", compare, one, 
  14.      zero, print_on
  15.       end;
  16.    
  17. feature {ANY}
  18.  
  19.    infix "+" (other: REAL): REAL is
  20.      -- Add `other' to Current.
  21.       external "CSE"
  22.       end;
  23.  
  24.    infix "-" (other: REAL): REAL is
  25.      -- Subtract `other' from Current.
  26.       external "CSE"
  27.       end;
  28.  
  29.    infix "*" (other: REAL): REAL is
  30.      -- Multiply `other' by Current.
  31.       external "CSE"
  32.       end;
  33.    
  34.    infix "/" (other: REAL): REAL is
  35.      -- Divide Current by `other'.
  36.       external "CSE"
  37.       end;
  38.  
  39.    infix "^" (exp : INTEGER): REAL is
  40.      -- Raise Current to `exp'-th power.
  41.       do
  42.      check
  43.         Current = 0.0 implies exp > 0;
  44.      end;
  45.      Result := (to_double ^ exp).to_real; 
  46.       end;
  47.  
  48.    prefix "+" : REAL is
  49.       do
  50.      Result := Current
  51.       end;
  52.  
  53.    prefix "-": REAL is
  54.       external "CSE"
  55.       end;
  56.  
  57.    abs: REAL is
  58.       do
  59.      if Current < 0.0 then
  60.         Result := -Current;
  61.      else
  62.         Result := Current;
  63.      end;
  64.       end;
  65.          
  66.    infix "<" (other: REAL): BOOLEAN is
  67.      -- Is Current less than `other'?
  68.       external "CSE"
  69.       end;
  70.    
  71.    infix "<=" (other: REAL): BOOLEAN is
  72.      -- Is Current smaller or equal to `other'?
  73.       external "CSE"
  74.       end;
  75.    
  76.    infix ">" (other: REAL): BOOLEAN is
  77.      -- Is Current greater than `other'?
  78.       external "CSE"
  79.       end;
  80.    
  81.    infix ">=" (other: REAL): BOOLEAN is
  82.      -- Is Current greater or equal to `other'?
  83.       external "CSE"
  84.       end;
  85.    
  86.    compare(other: REAL): INTEGER is
  87.      -- Compare Current with `other'.
  88.      -- '<' <==> Result < 0
  89.      -- '>' <==> Result > 0
  90.      -- Otherwise Result = 0
  91.       do
  92.      if Current < other then
  93.         Result := -1
  94.      else
  95.         if Current > other then
  96.            Result := 1
  97.         end;
  98.      end;
  99.       end;
  100.  
  101.    valid_divisor(other: REAL): BOOLEAN is
  102.       do
  103.      Result := (other /= 0.0)
  104.       end;
  105.    
  106.    one: REAL is 1.0;
  107.    
  108.    zero: REAL is 0.0;
  109.    
  110.    floor: INTEGER is
  111.      -- Greatest integral value no greater than Current.
  112.       do
  113.      Result := to_double.floor;
  114.       ensure 
  115.      result_no_greater: Current >= Result;
  116.      close_enough: Current - Result < one;
  117.       end;
  118.       
  119.    ceiling: INTEGER is
  120.      -- Smallest integral value no smaller than Current.
  121.       do
  122.      Result := floor + 1;
  123.       ensure
  124.      result_no_smaller: Current <= Result;
  125.      close_enough: Result.to_real - Current < one;
  126.       end;
  127.    
  128.    truncated_to_integer: INTEGER is
  129.      -- Integer part (same sign, largest absolute value
  130.      -- no greater than Current).
  131.       do
  132.      Result := floor;
  133.      if 0.5 + Result < Current then
  134.         Result := ceiling;
  135.      end;
  136.       end;
  137.       
  138.    to_string: STRING is
  139.      -- Convert the REAL into a new allocated STRING. 
  140.      -- Note: see `append_in' to save memory.
  141.       do
  142.      Result := to_double.to_string;
  143.       end; 
  144.  
  145.    append_in(str: STRING) is
  146.      -- Append the equivalent of `to_string' at the end of 
  147.      -- `str'. Thus you can save memory because no other
  148.      -- STRING is allocate for the job.
  149.       require
  150.      str /= Void;
  151.       do
  152.      to_double.append_in(str);
  153.       end; 
  154.    
  155.    to_string_format(d: INTEGER): STRING is
  156.      -- Convert the REAL into a new allocated STRING including 
  157.      -- only `d' digits in fractionnal part. 
  158.      -- Note: see `append_in_format' to save memory.
  159.       do
  160.      Result := to_double.to_string_format(d);
  161.       end; 
  162.  
  163.    append_in_format(str: STRING; f: INTEGER) is
  164.      -- Same as `append_in' but produce only `f' digit of 
  165.      -- the fractionnal part.
  166.       require
  167.      str /= Void;
  168.      f >= 0;
  169.       do
  170.      to_double.append_in_format(str,f);
  171.       end; 
  172.    
  173.    to_double: DOUBLE is
  174.       -- Note: C conversion from "float" to "double".
  175.       do
  176.      Result := Current;
  177.       end;
  178.    
  179.    sqrt: DOUBLE is
  180.            -- Compute the square routine.
  181.       require
  182.      Current >= 0;
  183.       do
  184.      Result := to_double.sqrt;
  185.       end;
  186.    
  187.    print_on(file: STD_FILE_WRITE) is
  188.       do
  189.      file.put_real(Current);
  190.       end;
  191.    
  192. end -- class REAL
  193.  
  194.